package com.mazaiting.generator.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mazaiting.common.core.domain.result.ResultCode;
import com.mazaiting.common.core.ex.exceptions.BusinessException;
import com.mazaiting.common.core.ex.exceptions.EmptyObjectException;
import com.mazaiting.generator.converter.IBaseClassConverter;
import com.mazaiting.generator.domain.entity.BaseClassEntity;
import com.mazaiting.generator.domain.form.BaseClassForm;
import com.mazaiting.generator.domain.query.BaseClassQuery;
import com.mazaiting.generator.domain.vo.BaseClassVO;
import com.mazaiting.generator.mapper.IBaseClassMapper;
import com.mazaiting.generator.service.IBaseClassService;
import com.mazaiting.mybatisplus.domain.vo.BasePageVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.Objects;
import java.util.Optional;

/**
 * 基类管理
 */
@Service
@RequiredArgsConstructor
public class BaseClassServiceImpl extends ServiceImpl<IBaseClassMapper, BaseClassEntity> implements IBaseClassService {

    private final IBaseClassConverter baseClassConverter;

    @Override
    public BasePageVO<BaseClassVO> page(BaseClassQuery baseClassQuery) {
        // 分页数据
        Page<BaseClassEntity> entityPage = lambdaQuery()
                .select(BaseClassEntity::getId, BaseClassEntity::getClassName, BaseClassEntity::getFields,
                        BaseClassEntity::getPackageName, BaseClassEntity::getRemark)
                .like(StringUtils.hasLength(baseClassQuery.getClassName()), BaseClassEntity::getClassName, baseClassQuery.getClassName())
                .page(new Page<>(baseClassQuery.getPageNum(), baseClassQuery.getPageSize()));
        // entity -> vo
        Page<BaseClassVO> voPage = baseClassConverter.entity2VO(entityPage);
        return BasePageVO.success(voPage);
    }

    @Override
    public boolean save(BaseClassEntity entity) {
        entity.setCreateTime(LocalDateTime.now());
        return super.save(entity);
    }

    @Override
    public boolean update(BaseClassForm baseClassForm) throws EmptyObjectException {
        // 根据 ID 查询数据是否存在
        BaseClassEntity baseClassEntity = getById(baseClassForm.getId());
        if (Objects.isNull(baseClassEntity)) {
            throw new EmptyObjectException(ResultCode.COMMON_FAIL.getCode(), "基类信息不存在");
        }
        // form -> entity
        baseClassConverter.form2Entity(baseClassForm, baseClassEntity);
        return updateById(baseClassEntity);
    }

    @Override
    public boolean save(BaseClassForm baseClassForm) throws BusinessException {
        // 根据包名与类名查询基类是否存在
        Optional<BaseClassEntity> baseClassEntityOpt = lambdaQuery()
                .eq(BaseClassEntity::getClassName, baseClassForm.getPackageName())
                .eq(BaseClassEntity::getClassName, baseClassForm.getClassName())
                .oneOpt();
        if (baseClassEntityOpt.isPresent()) {
            throw new BusinessException(ResultCode.COMMON_FAIL.getCode(), "基类信息已存在");
        }
        // 将 id 置空
        baseClassForm.setId(null);
        // form -> entity
        BaseClassEntity baseClassEntity = baseClassConverter.form2Entity(baseClassForm);
        return save(baseClassEntity);
    }

}